package fecat.ui.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


import com.jidesoft.grid.SortableTable;

import fecat.ui.contextpanel.table.ColoredTableCellRenderer;
import fecat.ui.contextpanel.table.ColumnData;
import fecat.util.SwingResourceManager;

public class ResizeTalble extends SortableTable{
	protected DefaultTableCellRenderer headrender;
	private ColumnData columns[] = {
		    new ColumnData( "Order No", 100, JLabel.LEFT ),
		    new ColumnData( "Account", 100, JLabel.LEFT ),
		    new ColumnData( "Type", 50, JLabel.RIGHT ),
		    new ColumnData( "Status", 50, JLabel.RIGHT ),
		    new ColumnData( "Currency", 100, JLabel.RIGHT )
		  };
	public ColumnData[] getColumns(){
		return columns;
	}
	public ResizeTalble(){
		 headrender = new ColoredTableCellRenderer();
	        headrender.setForeground(SwingResourceManager.getColor(255,255,255));
	        headrender.setBackground(SwingResourceManager.getColor(48,70,118));
		this.setModel(new QuoteTableModel());
		this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		this.createColumns();
		this.setAutoCreateColumnsFromModel(false);
		this.setColumnResizable(true);
		this.setRowResizable(true);
		this.setVariousRowHeights(true);

		this.setColumnAutoResizable(true);

		this.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		this.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	}
	static String[] QUOTE_COLUMNS = new String[]{"Symbol", "Name", "Last", "Change", "Volume"};

    static Object[][] QUOTES = new Object[][]{
            new Object[]{"AA", "ALCOA INC", "32.88", "+0.53 (1.64%)", "4156200"},
            new Object[]{"AIG", "AMER INTL GROUP", "69.53", "-0.58 (0.83%)", "4369200"},
            new Object[]{"AXP", "AMER EXPRESS CO", "48.90", "-0.35 (0.71%)", "4103600"},
            new Object[]{"BA", "BOEING CO", "49.14", "-0.18 (0.36%)", "3573700"},
            new Object[]{"C", "CITIGROUP", "44.21", "-0.89 (1.97%)", "28594900"},
            new Object[]{"CAT", "CATERPILLAR INC", "79.40", "+0.62 (0.79%)", "1458200"},
            new Object[]{"DD", "DU PONT CO", "42.62", "-0.14 (0.33%)", "1832700"},
            new Object[]{"DIS", "WALT DISNEY CO", "23.87", "-0.32 (1.32%)", "4443600"},
            new Object[]{"GE", "GENERAL ELEC CO", "33.37", "+0.24 (0.72%)", "31429500"},
            new Object[]{"GM", "GENERAL MOTORS", "43.94", "-0.20 (0.45%)", "3722100"},
            new Object[]{"HD", "HOME DEPOT INC", "34.33", "-0.18 (0.52%)", "5367900"},
            new Object[]{"HON", "HONEYWELL INTL", "35.70", "+0.23 (0.65%)", "4092100"},
            new Object[]{"HPQ", "HEWLETT-PACKARD", "19.65", "-0.25 (1.26%)", "11003000"},
            new Object[]{"IBM", "INTL BUS MACHINE", "84.02", "-0.11 (0.13%)", "6880500"},
            new Object[]{"INTC", "INTEL CORP", "23.15", "-0.23 (0.98%)", "95177008"},
            new Object[]{"JNJ", "JOHNSON&JOHNSON", "55.35", "-0.57 (1.02%)", "5428000"},
            new Object[]{"JPM", "JP MORGAN CHASE", "36.00", "-0.45 (1.23%)", "12135300"},
            new Object[]{"KO", "COCA COLA CO", "50.84", "-0.32 (0.63%)", "4143600"},
            new Object[]{"MCD", "MCDONALDS CORP", "27.91", "+0.12 (0.43%)", "6110800"},
            new Object[]{"MMM", "3M COMPANY", "88.62", "+0.43 (0.49%)", "2073800"},
            new Object[]{"MO", "ALTRIA GROUP", "48.20", "-0.80 (1.63%)", "6005500"},
            new Object[]{"MRK", "MERCK & CO", "44.71", "-0.97 (2.12%)", "5472100"},
            new Object[]{"MSFT", "MICROSOFT CP", "27.87", "-0.26 (0.92%)", "46717716"},
            new Object[]{"PFE", "PFIZER INC", "32.58", "-1.43 (4.20%)", "28783200"},
            new Object[]{"PG", "PROCTER & GAMBLE", "55.01", "-0.07 (0.13%)", "5538400"},
            new Object[]{"SBC", "SBC COMMS", "23.00", "-0.54 (2.29%)", "6423400"},
            new Object[]{"UTX", "UNITED TECH CP", "91.00", "+1.16 (1.29%)", "1868600"},
            new Object[]{"VZ", "VERIZON COMMS", "34.81", "-0.35 (1.00%)", "4182600"},
            new Object[]{"WMT", "WAL-MART STORES", "52.33", "-0.25 (0.48%)", "6776700"},
            new Object[]{"XOM", "EXXON MOBIL", "45.32", "-0.14 (0.31%)", "7838100"}
    };

    static class QuoteTableModel extends DefaultTableModel {
        public QuoteTableModel() {
            super(QUOTES, QUOTE_COLUMNS);
//            super(QUOTES,5);
//        	super()
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return true;
        }
    }
    protected void createColumns(){
		for(int k=0;k<this.getColumns().length;k++){
			DefaultTableCellRenderer renderer = new 
	        ColoredTableCellRenderer();
	      renderer.setHorizontalAlignment(
	    		  this.getColumns()[k].m_alignment);
	      TableColumn column = new TableColumn(k, 
	    		  this.getColumns()[k].m_width, renderer, null);
	      column.setHeaderRenderer(this.headrender);
	      this.addColumn(column);   
		}
	}
    public static void main(String[] args){
    	JPanel panel = new JPanel(new BorderLayout(6, 6));
    	JScrollPane sPane=new CustomScrollPane(new ResizeTalble());
//    	sPane.setViewport(new extendViewPort());
//    	sPane.setRowHeader(new extendViewPort());
//    	sPane.setColumnHeader(new extendViewPort());
//    	sPane.setViewport(new extendViewPort());
    	
    	panel.add(sPane, BorderLayout.CENTER);
    	panel.setPreferredSize(new Dimension(200,200));
    	JFrame mainFram=new JFrame();
    	mainFram.setLayout(new BorderLayout(6, 6));
    	mainFram.getContentPane().add(panel, BorderLayout.CENTER);
    	mainFram.setSize(new Dimension(500,500));
    	mainFram.toFront();
    	mainFram.setVisible(true);
    	}
}
class CustomViewPort extends JViewport{
	public void paint(Graphics g){
//		super.paint(g);
		Rectangle rect=this.getBounds();
		double height=rect.getHeight();
		double width=rect.getWidth();
		int index=0;
		for(int i=0;i<height;i++){
			if(index%2==0){
				g.setColor(new Color(20,46,76));
				g.fillRect(0,i,(int)width,15);
			}
			else{
				g.setColor(new Color(120,146,23));
				g.fillRect(0,i,(int)width,15);
			}
			index++;
			i+=15;
		}
		this.getComponent(0).repaint();
	}
}

class CustomScrollPane extends JScrollPane{
	public CustomScrollPane(JComponent comp) {
		//super(comp);
		CustomViewPort evp=new CustomViewPort();
		this.setViewport(evp);
		this.setColumnHeaderView(comp);
		evp.setBackground(new Color(70,15,190));
		evp.add(comp);
//		this.setColumnHeader(evp);
		this.setBackground(new Color(100,80,150));
//		this.getColumnHeader().setBackground(new Color(100,80,150));
	}
}